package com.why.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.why.entity.Result;
import com.why.entity.SerachWa;
import com.why.entity.Water;
import com.why.service.IWaterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴慧跃
 * @since 2022-05-27
 */
@RestController
@RequestMapping("/water")
public class WaterController {

    @Autowired
    IWaterService waterService;

    // 取款
    @RequestMapping("/takeWater")
    public Result takeWater(@RequestBody Water water){
        water.setId(null);
        water.setType("取款");
        water.setNowTime(LocalDateTime.now());
        if (water.getBalance()<water.getMoneyUser()){
            return new Result<String>(201,"","余额不足");
        }else {
            water.setMoneyEnd(water.getBalance()-water.getMoneyUser());
        }
        boolean b = waterService.saveOrUpdate(water);
        if (b){
            return new Result<String>(200,"","取款成功");
        }else {
            return new Result<String>(400,"取款失败","取款失败");
        }
    }

    // 存款
    @RequestMapping("/addWater")
    public Result addWater(@RequestBody Water water){
        water.setId(null);
        water.setType("存钱");
        water.setNowTime(LocalDateTime.now());
        if (water.getBalance()==null){
            water.setMoneyEnd(water.getMoneyUser());
        }else {
            water.setMoneyEnd(water.getBalance()+water.getMoneyUser());
        }
        System.err.println("存入后余额是 ---------- "+water.getMoneyEnd());
        boolean b = waterService.saveOrUpdate(water);
        if (b){
            return new Result<String>(200,"","存款成功");
        }else {
            return new Result<String>(400,"存款失败","存款失败");
        }
    }

    // 流水
    @RequestMapping("/listWater")
    public Page<Water> listWater(@RequestBody SerachWa wa){
        Page<Water> page = new Page<>(wa.getPage(),wa.getPageSize());
        QueryWrapper<Water> wrapper = new QueryWrapper<>();
        if (wa.getAcid()!=null){
            wrapper.eq("acid",wa.getAcid());
        }
        Page<Water> page1 = waterService.page(page, wrapper);
        return page1;
    }

}
